<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\IOFactory;

/**
 * 订单损益管理
 *
 * @icon fa fa-circle-o
 */
class Orderprofit extends Backend
{

    /**
     * Orderprofit模型对象
     * @var \app\admin\model\Orderprofit
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Orderprofit;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function add()
    {
        $orderSettlementSheetsPath = '';
        //结算单元数据
        $settlementSheet = IOFactory::load($orderSettlementSheetsPath);
        $worksheetSettlement = $settlementSheet->getActiveSheet();
        $highestRowSettlement = $worksheetSettlement->getHighestRow(); // 总行数
        //$highestColumnSettlement = $worksheet->getHighestColumn(); // 总列数
        //var_dump($highestRowSettlement);die();
        $data = [];
        for ($i = 2; $i <= $highestRowSettlement; ++$i) {
            $orderIdS = strval($worksheetSettlement->getCell('E' . $i)->getValue()); //订单ID
            $profit= (new \app\admin\model\Profit)->where('order_no', $orderIdS)->find();

            //var_dump($orderIndexByOrderId);
            $item = [];
            $item['back_money'] = $worksheetSettlement->getCell('J' . $i)->getValue(); //总结算金额
            //var_dump( $orderIndexByOrderId[$orderIdS]['totalSettlementAmount']);die();
            $item['export_transport_fee'] = $worksheetSettlement->getCell('U' . $i)->getValue(); //卖家实际支付运费
            $item['tk_trade_fee'] = $worksheetSettlement->getCell('S' . $i)->getValue(); //平台交易手续费
            $item['tk_commission_fee'] = $worksheetSettlement->getCell('T' . $i)->getValue(); //平台佣金
            $item['sfp_fee'] = $worksheetSettlement->getCell('AL' . $i)->getValue(); //SFP服务费
            $item['added_value_fee'] = $worksheetSettlement->getCell('AM' . $i)->getValue(); //增值税(SST)
            $item['bonus_cash_fee'] = $worksheetSettlement->getCell('AN' . $i)->getValue(); //奖金返现服务费
            $item['super_coupon_fee'] = $worksheetSettlement->getCell('AP' . $i)->getValue(); //超级优惠券服务费
            $item['affiliate_commission_fee'] = $worksheetSettlement->getCell('AB' . $i)->getValue(); //联盟佣金
            /*                $orderIndexByOrderId[$orderIdS]['totalSettlementAmount'] = $totalSettlementAmount;//总结算金额
                            $orderIndexByOrderId[$orderIdS]['sellerShippingFee'] = $sellerShippingFee;//卖家实际支付运费
                            $orderIndexByOrderId[$orderIdS]['transactionFee'] = $transactionFee;//平台交易手续费
                            $orderIndexByOrderId[$orderIdS]['tkCommissionFee'] = $tkCommissionFee;*/

            if (empty($profit)){
                $data[] = $item;
            }else{
                \app\admin\model\Profit::update($item,['order_no'=>$item['order_no']]);
            }


        }
        if (!empty($data)){
            $profit= (new \app\admin\model\Profit)->saveAll($data);
        }

    }

    public function something()
    {
        $orderSettlementSheetsPath = '';
        //结算单元数据
        $settlementSheet = IOFactory::load($orderSettlementSheetsPath);
        $worksheetSettlement = $settlementSheet->getActiveSheet();
        $highestRowSettlement = $worksheetSettlement->getHighestRow(); // 总行数
        //$highestColumnSettlement = $worksheet->getHighestColumn(); // 总列数
        //var_dump($highestRowSettlement);die();
        $data = [];
        for ($i = 2; $i <= $highestRowSettlement; ++$i) {
            $orderIdS = strval($worksheetSettlement->getCell('E' . $i)->getValue()); //订单ID
            $profit= (new \app\admin\model\Profit)->where('order_no', $orderIdS)->find();

            //var_dump($orderIndexByOrderId);
            $item = [];
            $item['back_money'] = $worksheetSettlement->getCell('J' . $i)->getValue(); //总结算金额
            //var_dump( $orderIndexByOrderId[$orderIdS]['totalSettlementAmount']);die();
            $item['export_transport_fee'] = $worksheetSettlement->getCell('U' . $i)->getValue(); //卖家实际支付运费
            $item['tk_trade_fee'] = $worksheetSettlement->getCell('S' . $i)->getValue(); //平台交易手续费
            $item['tk_commission_fee'] = $worksheetSettlement->getCell('T' . $i)->getValue(); //平台佣金
            $item['sfp_fee'] = $worksheetSettlement->getCell('AL' . $i)->getValue(); //SFP服务费
            $item['added_value_fee'] = $worksheetSettlement->getCell('AM' . $i)->getValue(); //增值税(SST)
            $item['bonus_cash_fee'] = $worksheetSettlement->getCell('AN' . $i)->getValue(); //奖金返现服务费
            $item['super_coupon_fee'] = $worksheetSettlement->getCell('AP' . $i)->getValue(); //超级优惠券服务费
            $item['affiliate_commission_fee'] = $worksheetSettlement->getCell('AB' . $i)->getValue(); //联盟佣金
            /*                $orderIndexByOrderId[$orderIdS]['totalSettlementAmount'] = $totalSettlementAmount;//总结算金额
                            $orderIndexByOrderId[$orderIdS]['sellerShippingFee'] = $sellerShippingFee;//卖家实际支付运费
                            $orderIndexByOrderId[$orderIdS]['transactionFee'] = $transactionFee;//平台交易手续费
                            $orderIndexByOrderId[$orderIdS]['tkCommissionFee'] = $tkCommissionFee;*/

            if (empty($profit)){
                $data[] = $item;
            }else{
                \app\admin\model\Profit::update($item,['order_no'=>$item['order_no']]);
            }


        }
        if (!empty($data)){
            $profit= (new \app\admin\model\Profit)->saveAll($data);
        }

    }

}
